package cn.mazexal.exambg.services;

import cn.mazexal.exambg.common.models.PagedData;
import cn.mazexal.exambg.dataware.FinOrderMapper;
import cn.mazexal.exambg.dataware.FinOrderPayMapper;
import cn.mazexal.exambg.dataware.VipPriceGroupMapper;
import cn.mazexal.exambg.dataware.VipPriceMapper;
import cn.mazexal.exambg.models.converter.OrderConverter;
import cn.mazexal.exambg.models.converter.VipPriceConverter;
import cn.mazexal.exambg.models.db.FinOrderDO;
import cn.mazexal.exambg.models.db.VipPriceDO;
import cn.mazexal.exambg.models.db.VipPriceGroupDO;
import cn.mazexal.exambg.models.qry.OrderQry;
import cn.mazexal.exambg.models.vo.FinOrderVO;
import cn.mazexal.exambg.models.vo.VipPriceGroupVO;
import cn.mazexal.exambg.models.vo.VipPriceVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Mazexal
 * Date: 2025/6/29
 */
@Service
public class OrderService {

    @Resource
    private FinOrderMapper finOrderMapper;

    @Resource
    private OrderConverter orderConverter;

    @Resource
    private FinOrderPayMapper finOrderPayMapper;

    @Resource
    private VipPriceMapper vipPriceMapper;

    @Resource
    private VipPriceConverter vipPriceConverter;

    @Resource
    private VipPriceGroupMapper vipPriceGroupMapper;

    public List<VipPriceVO> getVipPriceList(Long groupId) {
        List<VipPriceDO> list = vipPriceMapper.selectAll(groupId);
        return list.stream().map(vipPriceConverter::toView).toList();
    }

    public boolean addVipPrice(VipPriceVO vipPriceVO) {
        VipPriceDO vipPriceDO = vipPriceConverter.toDb(vipPriceVO);
        Long now = System.currentTimeMillis();
        vipPriceDO.setCreateTime(now);
        return vipPriceMapper.insert(vipPriceDO) > 0;
    }

    public boolean updateVipPrice(VipPriceVO vipPriceVO) {
        VipPriceDO vipPriceDO = vipPriceConverter.toDb(vipPriceVO);
        return vipPriceMapper.updateById(vipPriceDO) > 0;
    }

    public List<VipPriceGroupVO> getVipPriceGroupList(){
        return vipPriceGroupMapper.selectAll().stream().map(vipPriceConverter::toGroupView).collect(Collectors.toList());
    }

    public boolean addVipPriceGroup(VipPriceGroupVO vipPriceGroupVO) {
        VipPriceGroupDO vipPriceGroupDO = vipPriceConverter.toGroupDb(vipPriceGroupVO);
        vipPriceGroupDO.setCreateTime(System.currentTimeMillis());
        return vipPriceGroupMapper.insert(vipPriceGroupDO) > 0;
    }

    public boolean updateVipPriceGroup(VipPriceGroupVO vipPriceGroupVO) {
        VipPriceGroupDO vipPriceGroupDO = vipPriceConverter.toGroupDb(vipPriceGroupVO);
        return vipPriceGroupMapper.updateById(vipPriceGroupDO) > 0;
    }

    public PagedData<FinOrderVO> getOrderList(OrderQry qry) {
        List<FinOrderDO> finOrderDOList = finOrderMapper.getOrderList(qry);
        int count = finOrderMapper.getOrderCount(qry);
        return PagedData.of(qry, count, finOrderDOList.stream().map(orderConverter::toOrderView).toList());
    }

}
